package com.cencat.framework.core.controller;

import com.cencat.framework.core.dto.PageQuery;
import com.cencat.framework.core.dto.PageResult;
import com.cencat.framework.core.dto.Result;
import com.cencat.framework.core.entity.BaseEntity;
import com.cencat.framework.core.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public abstract class BaseController<T extends BaseEntity> {
    
    @Autowired
    private BaseService<T> baseService;
    
    @GetMapping("/{id}")
    public Result<T> getById(@PathVariable Long id) {
        T entity = baseService.getById(id);
        return Result.success(entity);
    }
    
    @GetMapping("/list")
    public Result<List<T>> list() {
        List<T> list = baseService.list();
        return Result.success(list);
    }
    
    @PostMapping("/page")
    public Result<PageResult<T>> page(@RequestBody PageQuery pageQuery) {
        PageResult<T> pageResult = baseService.page(pageQuery);
        return Result.success(pageResult);
    }
    
    @PostMapping
    public Result<Boolean> save(@RequestBody T entity) {
        boolean result = baseService.save(entity);
        return Result.success(result);
    }
    
    @PutMapping
    public Result<Boolean> update(@RequestBody T entity) {
        boolean result = baseService.updateById(entity);
        return Result.success(result);
    }
    
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = baseService.removeById(id);
        return Result.success(result);
    }
}